import com.fz.entity.Book;
import com.fz.entity.Shopaddress;
import com.fz.entity.Shopingcar;
import com.fz.mapper.BookMapper;
import com.fz.mapper.ShopaddressMapper;
import com.fz.mapper.ShopingcarMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by webrx on 2017/6/27.
 */
public class Dom {
    protected SqlSessionFactory sf;
    protected SqlSession ss;
    protected BookMapper bdao;
    protected ShopingcarMapper shop;
    protected ShopaddressMapper addre;



    @Before
    public void init(){

        try {
            this.sf=new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
            this.ss=this.sf.openSession();
            bdao= this.ss.getMapper(BookMapper.class);
            shop =this.ss.getMapper(ShopingcarMapper.class);
            addre= this.ss.getMapper(ShopaddressMapper.class);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @After
    public void  close(){
        this.ss.commit();
        this.ss.close();
    }
    @Test
    public void test(){
        List<Book> books = bdao.query();
        for(Book book : books){
            System.out.println(book.getName());
        }

        Book book = bdao.queryById(1);
        System.out.println(book);
    }
     @Test
    public void test02(){
       /*  Map<String,Object> map = new HashMap<String, Object>();
         map.put("name","王五");

       List<Shopingcar> list = shop.queryByName(map);
       for(Shopingcar sh: list){
           System.out.println(sh.getShopname());
       }*/
       /*int aa =shop.deleteById(4);
         System.out.println(aa);*/
         Map<String,Object> map = new HashMap<String, Object>();
         map.put("id",3);
         map.put("count",2);
         int dd =shop.updateById(map);
         System.out.println(dd);
    }
    @Test
    public void test03(){
       /* Map<String,Object> map = new HashMap<String, Object>();
        map.put("account","xiaoqiang");
        map.put("name","xiaoli");
        map.put("phone","123456789");
        map.put("address","郑州");
       int nn= addre.add(map);
        System.out.println(nn);*/

        Shopaddress address = new Shopaddress();
        address.setUseraccount("one");
        address.setUsername("你好吗");
        address.setUserphone("123456789");
        address.setUseraddress("郑州");
        addre.add02(address);

        List<Shopaddress> list = addre.query();
        for(Shopaddress add: list){
            System.out.println(add.getUsername());
        }
    }


}
